﻿using CommonBaseEntityDb.Db;
using Microsoft.Extensions.Configuration;
using Untils;

namespace TemplateApiCore.Config
{
    public class DbConfigHelper
    {
        /// <summary>
        /// 初始化数据库信息
        /// </summary>
        /// <param name="Configuration"></param>
        public static void InitDbConfig(IConfiguration Configuration)
        {
            #region 测试库
            //第一步：创建测试数据库实体
            DbOption dbServiceTest = new DbOption();
            //第二步：根据TestDb获取对应config配置中的信息绑定到创建的实体中
            Configuration.GetSection($"{DbOption.TestDb}").Bind(dbServiceTest);
            if (!string.IsNullOrEmpty(dbServiceTest.Encryption) && dbServiceTest.Encryption.ToUpper() == "TRUE")
                TestSugarConfig.ConnectionString = EncryptHelper.DESDecrypt(dbServiceTest.ConnectionString);            
            else
                TestSugarConfig.ConnectionString = dbServiceTest.ConnectionString;            

            TestSugarConfig.CommandTimeOut = dbServiceTest.CommandTimeOut;
            TestSugarConfig.SqlTrace = bool.Parse(dbServiceTest.SqlTrace);
            #endregion

            #region 模板库
            //第一步：创建测试数据库实体
            DbOption dbServiceTemplate = new DbOption();
            //第二步：根据TemplateDb获取对应config配置中的信息绑定到创建的实体中
            Configuration.GetSection($"{DbOption.TemplateDb}").Bind(dbServiceTemplate);
            if (!string.IsNullOrEmpty(dbServiceTemplate.Encryption) && dbServiceTemplate.Encryption.ToUpper() == "TRUE")
                TemplateSugarConfig.ConnectionString = EncryptHelper.DESDecrypt(dbServiceTemplate.ConnectionString);
            else
                TemplateSugarConfig.ConnectionString = dbServiceTemplate.ConnectionString;

            TemplateSugarConfig.CommandTimeOut = dbServiceTemplate.CommandTimeOut;
            TemplateSugarConfig.SqlTrace = bool.Parse(dbServiceTemplate.SqlTrace);
            #endregion
        }
    }
}
